<?php
namespace app\admin\controller;

use app\common\model\BookChapter as BookChapterModel;
/**
 * 章节控制器
 * Class BookChapter
 * @package app\index\controller
 */
class BookChapter extends Base
{
    /**
     * [getIndex  章节列表]
     * @return mixed|string
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new BookChapterModel();
        $map = [];
        $user = $this->user;
        try{
            if (!empty($param['con']))
                $map['chapter'] = ['like', "%" . $param['con'] . "%"];
            if(!empty($param['start']) && !empty($param['end']))
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            $map['status'] = ['neq',2];
            $map['bookid'] = $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/BookChapter/getIndex');
            return admin_json(-1,'查看章节失败'.$e->getMessage());
        }
        writelog('查看章节列表', 0,$user);
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  章节添加]
     * @return mixed|string
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new BookChapterModel();
        $user = $this->user;
        try{
            $param['total'] = mb_strlen(strip_tags($param['content']));
            $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/BookChapter/postAdd');
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        writelog('添加章节', 0,$user);
        return admin_json(1,'添加成功');
    }
    /**
     * [postAdd  章节修改]
     * @return mixed|string
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new BookChapterModel();
        $user = $this->user;
        try{
            $param['total'] = mb_strlen(strip_tags($param['content']));
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/BookChapter/postEdit');
            return admin_json(-1,'修改失败'.$e->getMessage());
        }
        writelog('修改章节id:'.$param['id'], 0,$user);
        return admin_json(1,'修改成功');
    }
    /**
     * [getDel  章节删除]
     * @return mixed|string
     * @author [C。]
     */
    public function getDel()
    {
        $param = input('param.');
        $model = new BookChapterModel();
        $user = $this->user;
        try{
            $model->editData(['status' => 2,'id' => $param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/BookChapter/getDel');
            return admin_json(-1,'删除失败'.$e->getMessage());
        }
        writelog('删除章节id:'.$param['id'], 0,$user);
        return admin_json(1,'删除成功');
    }
    /**
     * [getDel  章节删除]
     * @return mixed|string
     * @author [C。]
     */
    public function postDealtxt()
    {
        $param = input('param.');
        $model = new BookChapterModel();
        try{
            if (!empty($_FILES['file']['name'])) {
                $fileName = $_FILES['file']['name'];    //得到文件全名
                $dotArray = explode('.', $fileName);    //把文件名安.区分，拆分成数组
                $type = end($dotArray);
                if ($type != "txt") {
                    return admin_json(0, '不是txt文件，请重新上传!');
                }
                //取数组最后一个元素，得到文件类型
                $uploaddir = "txt/" . date("Y-m-d") . '/';//设置文件保存目录 注意包含

                if (!file_exists($uploaddir)) {
                    mkdir($uploaddir, 0755, true);
                }

                $path = $uploaddir . md5(uniqid(rand())) . '.' . $type; //产生随机文件名
                //$path = "images/".$fileName; //客户端上传的文件名；
                //下面必须是tmp_name 因为是从临时文件夹中移动
                move_uploaded_file($_FILES['file']['tmp_name'], $path); //从服务器临时文件拷贝到相应的文件夹下

                $file_path = $path;
                if (!file_exists($path)) {
                    $ret['res'] = "0";
                    $ret['msg'] = "上传文件丢失!" . $_FILES['excel']['error'];
                    return json_encode($ret);
                }
            }
            $chapter_arr = dealbook('./'.$file_path);
            foreach ($chapter_arr as &$v){
                $v['total'] = mb_strlen(strip_tags($v['content']));
                $v['bookid'] = $param['bookid'];
            }
            $model->addDataAll($chapter_arr);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/BookChapter/postDealtxt');
            return admin_json(-1,'上传失败'.$e->getMessage());
        }
        return admin_json(1,'上传成功',$chapter_arr);
    }
}